<?php
//管理端用户密码重置

namespace App\Console\Commands;

use App\Services\ManageUserService;
use App\Services\Wx\WxConfig;
use App\Services\Wx\WxCoupon;
use Illuminate\Console\Command;

class ManageUserPasswordReset extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'manage:user:password:reset {user_id : 操作类型}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '管理端用户密码重置';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $userId = $this->argument('user_id');
        if (!$userId) {
            exit('error');
        }
        $userInfo = ManageUserService::getUsreInfoById($userId);
        if (!$userInfo) {
            exit('用户有误');
        }
        $newPassword = $this->getRandPassword();
        $res = ManageUserService::updatePassword($userId, $newPassword, $userInfo['salt']);
        if (!$res) {
            exit('充值失败');
        }

        dd('重置成功，新密码：'.$newPassword);

        return true;
    }

    /**
     * 随机生成密码
     * @return string
     */
    public function getRandPassword()
    {
        $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
        $key = '';
        for ($i = 0; $i < 6; $i++) {
            $key .= $pattern[mt_rand(0, 35)];//生成php随机数
        }

        return $key;
    }


}
